<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">

				<el-form-item label="城市">
					<el-input v-model="filters.taskCity" placeholder="城市"></el-input>
				</el-form-item>
				<el-form-item label="状态"   >
					<el-select v-model="filters.state" placeholder="状态"   >
						<el-option value="" label="全部" >全部</el-option>
						<el-option value="1" label="使用" >使用</el-option>
						<el-option value="0" label="禁用" >禁用</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPageTask">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  @click="handleAdd">新增</el-button>
				</el-form-item>

			</el-form>
		</el-col>
		<!--列表-->
		<template>
			<el-table :data="tasks" highlight-current-row v-loading="loading" border max-height="620" style="width: 100%;">
				<el-table-column type="index" width="80" label="编号">
				</el-table-column>
				<el-table-column prop="province" label="省份" >
				</el-table-column>
				<el-table-column prop="taskCity" label="城市" :formatter="formatTaskCity" >
				</el-table-column>
				<el-table-column prop="taskAmount" label="任务金额" >
				</el-table-column>
				<el-table-column prop="rechargeAmount" label="已充值金额" >
				</el-table-column>
				<el-table-column prop="state" label="状态"   :formatter="formatStatus" >
				</el-table-column>

				<el-table-column label="操作"    width="220">
					<template scope="scope"   >
						<el-button size="small" v-show="isShowSupper"  @click="handleEdit(scope.$index, scope.row)">
						编辑
						</el-button>
						<el-button size="small"   @click="handleEnable(scope.$index, scope.row)">
							{{scope.row.state == 0 ? '启用' : '禁用'}}
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>
		<!--新增界面-->
		<el-dialog title="新增充值账号"  width="30%" v-model="addFormVisible" :close-on-click-modal="false">


			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">

				<el-form-item label="省份"   prop="province">
					<el-select v-model="addForm.province" placeholder="请选择省份"  @change="provinceChange"  >
						<el-option
								v-for="item in provinces"
								:key="item"
								:label="item"
								:value="item">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="城市"  prop="taskCity"  >
					<el-select v-model="addForm.taskCity" placeholder="请选择城市"   >
						<el-option value="0" label="全省" >全省</el-option>
						<el-option
								v-for="item in citys"
								:key="item"
								:label="item"
								:value="item">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="限制金额" prop="taskAmount">
					<el-input v-model="addForm.taskAmount" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="编辑充值账号"  width="30%" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="省份"   prop="province">
					<el-select v-model="editForm.province" placeholder="请选择省份"  @change="provinceChange"  >
						<el-option
								v-for="item in provinces"
								:key="item"
								:label="item"
								:value="item">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="城市"  prop="taskCity"  >
					<el-select v-model="editForm.taskCity" placeholder="请选择城市"   >
						<el-option value="0" label="全省" >全省</el-option>
						<el-option
								v-for="item in citys"
								:key="item"
								:label="item"
								:value="item">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="限制金额" prop="taskAmount">
					<el-input v-model="editForm.taskAmount" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>

</template>
<script>
	import {findPageTask ,findAllProvince,findAllCity,saveTask,updateTask,updateTaskState} from '../../api/api';
    import RSAUtils from '../../common/security'
    var crypto = require('crypto');
	export default {
		data() {
			return {
				filters: {
                    taskCity: '',
					state:''
				},
                isShowSupper:JSON.parse(sessionStorage.getItem("user")).usertype === 2 ? true : false,
				start:0,
				pageSize:10,
                total:0,
                getCode:'获取验证码',
				loading: false,
                tasks: [],
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                provinces:[],
                citys:[],
                addFormRules: {
                    province: [
                        { required: true, message: '请选择省份', trigger: 'blur' },
                    ],taskAmount: [
                        { required: true, message: '请输入限制金额,单位 元', trigger: 'blur' },
                    ],taskCity: [
                        { required: true, message: '请选择城市', trigger: 'blur' },
                    ]
                },
                //新增界面数据
                addForm: {
                    province:'',
                    taskAmount:'',
                    taskCity:''
                },

                editFormVisible: false,
                editLoading: false,
                editFormRules: {
                    province: [
                        { required: true, message: '请选择省份', trigger: 'blur' },
                    ],taskAmount: [
                        { required: true, message: '请输入限制金额,单位 元', trigger: 'blur' },
                    ],taskCity: [
                        { required: true, message: '请选择城市', trigger: 'blur' },
                    ]
                },
                editForm: {
                    province:'',
                    taskAmount:'',
                    taskCity:''
                }
			}
		},
		methods: {
            formatStatus: function (row, column) {
                return row.state === 1 ? '使用' : row.state === 0 ? '禁用' : "其他";
            },
            formatTaskCity: function (row, column) {
                return row.taskCity == "0" ? '全省' : row.taskCity;
            },
            handleAdd:function(){
				this.addFormVisible = true;
              	this.findAllProvince();
			},findAllProvince:function () {
                findAllProvince().then((datas) => {
                    this.addLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    }
                    this.provinces = data;
                });
            }
			,provinceChange: function (province) {
                this.addForm.province = province;
				this.findAllCity(province);
            },findAllCity:function (province) {
                let para = {
                    province : province
                };
                findAllCity(para).then((datas) => {
                    this.addLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    }
                    this.citys = data;
                });
            },
            addSubmit:function(){
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            saveTask(para).then((datas) => {
                                this.addLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.findPageTask();
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                            }).catch(function (error) {
                                console.log(error);
                                this.addLoading = false;
                            });
                        });
                    }
                });
			},
            findPageTask: function () {
				let para = {
                    taskCity: this.filters.taskCity,
					start:this.start,
					pageSize:this.pageSize,
					state : this.filters.state
				};
				this.loading = true;
                findPageTask(para).then(datas => {
                    this.loading = false;
                    //NProgress.done();
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.tasks = data.result;
                        this.total = data.totalCount;
                    }
                }).catch(e =>{

				});
			},handleCurrentChange:function(val){
                this.start = val - 1;
                this.findPageTask();
			},
            handleSizeChange:function(val){
			    this.pageSize = val;
			    this.findPageTask();
			} ,handleEdit:function(index, row){
                this.editFormVisible = true;
                this.findAllProvince();
                this.findAllCity(  row.province);
                this.editForm = Object.assign({}, row);
			},editSubmit(){
                let para = Object.assign({}, this.editForm);
                this.$confirm('确认编辑该账号信息吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.editFormVisible = true;
                    updateTask(para).then((res) => {
                        this.editFormVisible = false;
                        let { message, code, data } = res;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        } else {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        }
                        this.findPageTask();
                    });
                });
            },handleEnable: function (index, row) {
                this.$confirm('确认更新该账号状态吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = {
                        state: row.state == "0" ? "1" : "0",
                        taskId: row.taskId
                    };
                    updateTaskState(para).then((res) => {
                        this.listLoading = false;
                        let { message, code, data } = res;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        } else {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        }
                        this.findPageTask();
                    });
                }).catch(() => {

                });
            }
		},

		mounted() {
			this.findPageTask();
		},
    	beforeCreate: function (){
//            window.location.reload();
		}
	};

</script>

<style scoped>

</style>
